package org.erp.product.controller;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.erp.common.domain.Result;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.product.infrastructure.convert.InspectionTransferConvert;
import org.erp.product.infrastructure.convert.RepairTransferConvert;
import org.erp.product.domain.bo.ConnectToInnerAddBo;
import org.erp.product.domain.bo.InspectionTransferLogQueryBo;
import org.erp.product.domain.bo.TransferLogQueryBo;
import org.erp.product.domain.vo.ProductInfoVo;
import org.erp.product.service.InspectionTransferLogService;
import org.erp.product.service.RepairTransferLogService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.erp.common.domain.Result.success;

/**
 * 调拨控制层
 *
 * @author libin
 * @version 1.0
 * @description: 调拨控制层
 * @date 2025/11/14 17:56
 **/
@Log4j2
@RestController
@AllArgsConstructor
@RequestMapping("/transfer")
public class TransferLogController {

    private final RepairTransferLogService repairTransferLogService;

    private final InspectionTransferLogService inspectionTransferLogService;

    private final InspectionTransferConvert inspectionTransferConvert;

    private final RepairTransferConvert repairTransferConvert;

    /**
     * 维修调拨列表
     *
     * @param transferLogQueryBo
     * @return
     */
    @PostMapping("/repair/list")
    public Result<DataPageResp<ProductInfoVo>> repairPageList(@RequestBody TransferLogQueryBo transferLogQueryBo) {
        PageQueryModel<ProductInfoVo> pageList = repairTransferLogService.pageList(repairTransferConvert.boConvertDto(transferLogQueryBo));
        return success(DataPageResp.of(pageList.getTotal(), pageList.getDataList()));
    }

    /**
     * 维修调拨
     *
     * @param connectToInnerAddBo
     * @return
     */
    @PutMapping("/repair/transfer")
    public Result<String> repairTransfer(@RequestBody ConnectToInnerAddBo connectToInnerAddBo) {
        repairTransferLogService.transfer(repairTransferConvert.boConvertDto(connectToInnerAddBo));
        return success();
    }

    /**
     * 维修返库
     *
     * @param stockIds
     * @return
     */
    @PutMapping("/repair/returnBack")
    public Result<String> repairReturnBack(@RequestParam List<Long> stockIds) {
        repairTransferLogService.returnBack(stockIds);
        return success();
    }

    /**
     * 质检调拨列表
     *
     * @param inspectionTransferLogQueryBo
     * @return
     */
    @PostMapping("/list")
    public Result<DataPageResp<ProductInfoVo>> inspectionPageList(@RequestBody InspectionTransferLogQueryBo inspectionTransferLogQueryBo) {
        PageQueryModel<ProductInfoVo> pageList = inspectionTransferLogService.pageList(inspectionTransferConvert.boConvertDto(inspectionTransferLogQueryBo));
        return success(DataPageResp.of(pageList.getTotal(), pageList.getDataList()));
    }

    /**
     * 上架
     */
    @PutMapping("onShelf")
    public Result<String> onShelf(@RequestParam List<Long> ids) {
        inspectionTransferLogService.onShelf(ids);
        return success();
    }


    /**
     * 下架
     */
    @PutMapping("offShelf")
    public Result<String> offShelf(@RequestParam List<Long> ids) {
        inspectionTransferLogService.offShelf(ids);
        return success();
    }

    /**
     * 调拨
     *
     * @param connectToInnerAddBo
     * @return
     */
    @PutMapping("/inspection/transfer")
    public Result<String> inspectionTransfer(@RequestBody ConnectToInnerAddBo connectToInnerAddBo) {
        inspectionTransferLogService.transfer(inspectionTransferConvert.boConvertDto(connectToInnerAddBo));
        return success();
    }

    /**
     * 返库
     *
     * @param stockIds
     * @return
     */
    @PutMapping("/inspection/returnBack")
    public Result<String> inspectionReturnBack(@RequestParam List<Long> stockIds) {
        inspectionTransferLogService.returnBack(stockIds);
        return success();
    }
}
